package com.example.elasticsearch.controller;

import cn.hutool.core.lang.UUID;
import com.example.elasticsearch.entity.Commodity;
import com.example.elasticsearch.repository.CommodiousRepository;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Author syz
 * @Date 2020-09-17  16:50
 * @Description: 商品前端控制器
 */
@RestController
@RequestMapping("/commodity")
public class CommodiousController {

    @Autowired
    private CommodiousRepository commodiousRepository;

    @GetMapping("/save")
    public String save() {
        Commodity commodity = new Commodity();
        commodity.setId(UUID.fastUUID().toString());
        commodity.setName("iphone11 MAX");
        commodity.setPrice(new BigDecimal(7825));
        commodiousRepository.save(commodity);
        return "success";
    }

    //每页数量
    private static final Integer PAGE_SIZE = 10;

    //http://localhost:8888/getGoodsList?query=商品
    //http://localhost:8888/getGoodsList?query=商品&pageNumber=1
    //根据关键字"商品"去查询列表，name或者description包含的都查询
    @GetMapping("getCommodiousList")
    public List<Commodity> getList(Integer pageNumber, String query) {
        pageNumber = pageNumber == null ? 0 : pageNumber;
        //es搜索默认第一页页码是0
        //SearchQuery
        SearchQuery searchQuery = getEntitySearchQuery(pageNumber, PAGE_SIZE, query);
        Page<Commodity> goodsPage = CommodiousRepository.search(searchQuery);
        return goodsPage.getContent();
    }

    private NativeSearchQuery getEntitySearchQuery(int pageNumber, int pageSize, String searchContent) {
        FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery()
                .add(QueryBuilders.matchPhraseQuery("name", searchContent),
                        ScoreFunctionBuilders.weightFactorFunction(100))
                .add(QueryBuilders.matchPhraseQuery("description", searchContent),
                        ScoreFunctionBuilders.weightFactorFunction(100))
                //设置权重分 求和模式
                .scoreMode("sum")
                //设置权重分最低分
                .setMinScore(10);

        // 设置分页
        Pageable pageable = new PageRequest(pageNumber, pageSize);
        return new NativeSearchQueryBuilder()
                .withPageable(pageable)
                .withQuery(functionScoreQueryBuilder).build();
    }

    public static void main(String[] args) {

    }
}
